package com.roadjava.rbac.service.impl;

import com.roadjava.rbac.bean.entity.BannerRes;
import com.roadjava.rbac.bean.entity.IndexRes;
import com.roadjava.rbac.bean.req.resource.AddBannerResParam;
import com.roadjava.rbac.mapper.BannerResMapper;
import com.roadjava.rbac.service.BannerResService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;


@Service
public class BannerResServiceImpl implements BannerResService {
    @Resource
    BannerResMapper bannerResMapper;
    @Value("${upload.static-dir}")
    private String getUploadDir;

    private static final String UPLOAD_DIR = "uploads";
    @Override
    public String addRes(AddBannerResParam param) {
        BannerRes bannerRes = new BannerRes();
        bannerRes.setName(param.getFileName());
        bannerRes.setPath(UPLOAD_DIR+"/"+param.getFileName());
        bannerRes.setLink(param.getLink());
        bannerRes.setType(param.getType());
        bannerRes.setTime(param.getTime());
        bannerResMapper.insert(bannerRes);
        return "添加成功";
    }

    @Override
    public String uploadFile(MultipartFile file) throws IOException {
        // 创建文件保存目录
//        String uploadDir = System.getProperty("user.dir") + File.separator + UPLOAD_DIR;
        String uploadDir = getUploadDir + File.separator + UPLOAD_DIR;
        Path uploadPath = Paths.get(uploadDir);
//        Path uploadPath = Paths.get(UPLOAD_DIR);
        if (!Files.exists(uploadPath)) {
            Files.createDirectories(uploadPath);
        }

        // 获取文件名
        String fileName = file.getOriginalFilename();
        if (fileName == null) {
            throw new IllegalArgumentException("File name cannot be null");
        }

        // 保存文件
        Path filePath = uploadPath.resolve(fileName);
        System.out.println("Uploading to: " + filePath);
        file.transferTo(filePath.toFile());

        // 将文件信息存入数据库
//        IndexRes indexRes = new IndexRes();
//        indexRes.setName(fileName);
//        indexRes.setPath(UPLOAD_DIR+"/"+fileName);
//        indexResMapper.insert(indexRes);

        return fileName;
    }

    @Override
    public List<BannerRes> getAllFiles() {
        return bannerResMapper.getAllFiles();

    }

    @Override
    public String deleteFile(Integer id) {
        // 根据文件ID获取文件信息
        IndexRes indexRes = bannerResMapper.getFileById(id);
        if (indexRes == null) {
            return "File not found";
        }

        // 删除数据库中的记录
        bannerResMapper.deleteFileById(id);

        // 删除物理文件
        Path filePath = Paths.get(getUploadDir+indexRes.getPath());
        File file = filePath.toFile();
        if (file.exists()) {
            boolean deleted = file.delete();
            if (deleted) {
                return "File deleted successfully";
            } else {
                return "Failed to delete the file from the server";
            }
        } else {
            return "File does not exist on the server";
        }
    }

    @Override
    public List<BannerRes> getImgByType(Integer type) {
        return bannerResMapper.getImgByType(type);
    }
}
